﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using XiaoGang.Soft.XGWMS.COM;

namespace XiaoGang.Soft.XGWMS.DAL_SQL
{
    public class DAL_M006_GOODS_GROUP
    {
        #region DAL_M006_GOODS_GROUP

        /// <summary>	
        /// <p><b>方法名称</b>GetSingleData</p>	
        /// <p><b>概要説明  :</b>获取单条数据</p>	
        /// <p><b>作成者    :</b>万岗</p>	
        /// <p><b>作成日    :</b>2008/08/26</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="string" name="strGOODS_GROUP_ID">商品分类编号</param>
        /// <returns type = "Boolean">　True:存在   False:不存在 </returns>
        public ReturnValue GetSingleData(MODEL.MOD_M006_GOODS_GROUP model)
        {
            SqlParameter parameter = new SqlParameter("@GOODS_GROUP_ID", SqlDbType.Char, 12);
            parameter.Value = model.GOODS_GROUP_ID;

            return XGWMS.DBUtility.SqlHelper.Query(DAL_M006_GOODS_GROUP_SQL.Get_Single_Data_SQL,parameter);
        }

        /// <summary>	
        /// <p><b>方法名称</b>ExistProcess</p>	
        /// <p><b>概要説明  :</b>データを存在のCheck</p>	
        /// <p><b>作成者    :</b>万岗</p>	
        /// <p><b>作成日    :</b>2008/08/26</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="string" name="strGOODS_GROUP_ID">商品分类编号</param>
        /// <returns type = "Boolean">　True:存在   False:不存在 </returns>
        public ReturnValue ExistProcess(string strGoodsGroupID)
        {
            SqlParameter parameter = new SqlParameter("@GOODS_GROUP_ID", SqlDbType.Char,12);
            parameter.Value = strGoodsGroupID;

            return XGWMS.DBUtility.SqlHelper.Query(DAL_M006_GOODS_GROUP_SQL.Exist_GOODS_GROUP_SQL, parameter);
        }

        /// <summary>	
        /// <p><b>方法名称</b>CheckName</p>	
        /// <p><b>概要説明  :</b>检查同ID但不同名称的商品分类</p>	
        /// <p><b>作成者    :</b>万岗</p>	
        /// <p><b>作成日    :</b>2008/08/26</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="class" name="model">商品分类信息</param>
        /// <returns type = "ReturnValue"></returns>
        public ReturnValue CheckGoodsGroupNameWithoutID(string GroupID,string GroupName)
        {
            string strWhere = string.Empty;
            if (GroupID.Length > 0)
            {
                strWhere += " and GOODS_GROUP_ID <> '"+GroupID+"'";
            }
            if (GroupName.Length > 0)
            {
                strWhere += " and GOODS_GROUP_NM = '"+GroupName+"'";
            }
            return XGWMS.DBUtility.SqlHelper.Query(DAL_M006_GOODS_GROUP_SQL.Check_Name_SQL+strWhere);
        }

        /// <summary>	
        /// <p><b>方法名称</b>CheckName</p>	
        /// <p><b>概要説明  :</b>检查商品分类名称是否存在</p>	
        /// <p><b>作成者    :</b>万岗</p>	
        /// <p><b>作成日    :</b>2008/08/26</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="class" name="model">商品分类信息</param>
        /// <returns type = "ReturnValue"></returns>
        public ReturnValue CheckName(string GroupName)
        {
            return CheckGoodsGroupNameWithoutID("",GroupName);
        }

        /// <summary>	
        /// <p><b>方法名称</b>GoodsGroupDeleteCheck</p>	
        /// <p><b>概要説明  :</b>检测该分类有没有没使用</p>	
        /// <p><b>作成者    :</b>万岗</p>	
        /// <p><b>作成日    :</b>2008/09/17</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="class" name="model">商品分类编号</param>
        /// <returns type = "ReturnValue"></returns>
        public ReturnValue GoodsGroupDeleteCheck(string strGoodsGroupID)
        {
            SqlParameter parameter = new SqlParameter("@GOODS_GROUP", SqlDbType.Char, 12);
            parameter.Value = strGoodsGroupID.Trim();

            return XGWMS.DBUtility.SqlHelper.Query(DAL_M006_GOODS_GROUP_SQL.Goods_Group_Delete_Check_SQL, parameter);
        }

        /// <summary>	
        /// <p><b>方法名称</b>SelectProcess</p>	
        /// <p><b>概要説明  :</b>データを検索する</p>	
        /// <p><b>作成者    :</b>万岗</p>	
        /// <p><b>作成日    :</b>2008/08/26</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="string" name="strGOODS_GROUP_ID">商品分类编号</param>
        /// <returns type = "Boolean">　True:存在   False:不存在 </returns>
        public ReturnValue SelectProcess(MODEL.MOD_M006_GOODS_GROUP model,bool isShowDelete)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("SELECT  [CREATE_DATETIME] {0}",(char)13);
            strSql.AppendFormat("       ,[UPDATE_DATETIME] {0}",(char)13);
            strSql.AppendFormat("       ,[TRANS_STATE] {0}",(char)13);
            strSql.AppendFormat("       ,[UPDATER_ID] {0}",(char)13);
            strSql.AppendFormat("       ,[TERMINAL_CD] {0}",(char)13);
            strSql.AppendFormat("       ,[GOODS_GROUP_ID] {0}", (char)13);
            strSql.AppendFormat("       ,[GOODS_GROUP_NM] {0}",(char)13);
            strSql.AppendFormat("       ,[OrderNum] {0}", (char)13);
            strSql.AppendFormat("       ,[MEMO] {0}",(char)13);
            strSql.AppendFormat("FROM    [M006_GOODS_GROUP] {0}",(char)13);
            strSql.AppendFormat(" Where 1=1 {0}", (char)13);
            if (!isShowDelete)
            {
                strSql.AppendFormat("AND TRANS_STATE <> 3 {0}", (char)13);
            }
            if (0 != model.GOODS_GROUP_ID.Length)
            {
                strSql.AppendFormat("AND GOODS_GROUP_ID like '%{0}%' {1}", model.GOODS_GROUP_ID,(char)13);
            }
            if (0 != model.GOODS_GROUP_NM.Length)
            {
                strSql.AppendFormat("AND GOODS_GROUP_NM LIKE '%{0}%' {1}", model.GOODS_GROUP_NM, (char)13);
            }
            if (0 != model.MEMO.Length)
            {
                strSql.AppendFormat("AND MEMO LIKE '%{0}%' {1}", model.MEMO, (char)13);
            }
            strSql.AppendFormat("    ORDER BY OrderNum,GOODS_GROUP_ID {0}", (char)13);
            return XGWMS.DBUtility.SqlHelper.Query(strSql.ToString());
        }

        public ReturnValue SelectGoodsNoProcess ( string GoodsID )
        {
            string strSQL = @"SELECT ID,GoodsID,GoodsNo From M013_GoodSNo Where GoodsID = '"+GoodsID+"'";
            return XGWMS.DBUtility.SqlHelper.Query (strSQL);
        }


        /// <summary>	
        /// <p><b>方法名称</b>InsertProcess</p>	
        /// <p><b>概要説明  :</b>商品分类信息的插入</p>	
        /// <p><b>作成者    :</b>万岗</p>	
        /// <p><b>作成日    :</b>2008/08/26</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="class" name="model">商品分类信息</param>
        /// <returns type = "ReturnValue"></returns>
        public ReturnValue InsertProcess(MODEL.MOD_M006_GOODS_GROUP model)
        {        
            SqlParameter[] parameters = {
					new SqlParameter("@UPDATER_ID", SqlDbType.VarChar,12),
					new SqlParameter("@TERMINAL_CD", SqlDbType.VarChar,256),
					new SqlParameter("@GOODS_GROUP_ID", SqlDbType.Char,12),
					new SqlParameter("@GOODS_GROUP_NM", SqlDbType.VarChar,256),
					new SqlParameter("@MEMO", SqlDbType.VarChar,256),
                    new SqlParameter("@OrderNum", SqlDbType.Int)};
            parameters[0].Value = model.UPDATER_ID;
            parameters[1].Value = model.TERMINAL_CD;
            parameters[2].Value = model.GOODS_GROUP_ID;
            parameters[3].Value = model.GOODS_GROUP_NM;
            parameters[4].Value = model.MEMO;
            if (model.OrderNum.HasValue)
            {
                parameters[5].Value = model.OrderNum;
            }
            else
            {
                parameters[5].Value = DBNull.Value;
            }
            return XGWMS.DBUtility.SqlHelper.ExecuteSql(DAL_M006_GOODS_GROUP_SQL.Insert_GOODS_GROUP_SQL, parameters);
        }

        public ReturnValue InsertGoodsNoProcess ( string GoodsID,string GoodsNO )
        {
            SqlParameter[] parameters = {		
					new SqlParameter("@GOODS_ID", SqlDbType.VarChar,50),
					new SqlParameter("@GOODS_NO", SqlDbType.VarChar,50)};
            parameters[0].Value = GoodsID;
            parameters[1].Value = GoodsNO;

            return XGWMS.DBUtility.SqlHelper.ExecuteSql (DAL_M006_GOODS_GROUP_SQL.Insert_GOODS_NO_SQL, parameters);
        }

        public ReturnValue DeleteGoodsNoProcess ( string ID )
        {
            SqlParameter[] parameters = {new SqlParameter("@ID", SqlDbType.Int)};
            parameters[0].Value = ID;

            return XGWMS.DBUtility.SqlHelper.ExecuteSql (DAL_M006_GOODS_GROUP_SQL.Delete_GOODS_NO_SQL, parameters);
        }

        /// <summary>	
        /// <p><b>方法名称</b>InsertProcess</p>	
        /// <p><b>概要説明  :</b>商品分类信息的更新</p>	
        /// <p><b>作成者    :</b>万岗</p>	
        /// <p><b>作成日    :</b>2008/08/26</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="class" name="model">商品分类信息</param>
        /// <returns type = "ReturnValue"></returns>
        public ReturnValue UpdateProcess(MODEL.MOD_M006_GOODS_GROUP model)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@UPDATER_ID", SqlDbType.VarChar,12),
					new SqlParameter("@TERMINAL_CD", SqlDbType.VarChar,256),					
					new SqlParameter("@GOODS_GROUP_NM", SqlDbType.VarChar,256),
					new SqlParameter("@MEMO", SqlDbType.VarChar,256),
                    new SqlParameter("@OrderNum", SqlDbType.Int),
                    new SqlParameter("@GOODS_GROUP_ID", SqlDbType.Char,12)};
            parameters[0].Value = model.UPDATER_ID;
            parameters[1].Value = model.TERMINAL_CD;            
            parameters[2].Value = model.GOODS_GROUP_NM;
            parameters[3].Value = model.MEMO;
            if (model.OrderNum.HasValue)
            {
                parameters[4].Value = model.OrderNum;
            }
            else
            {
                parameters[4].Value = DBNull.Value;
            }
            parameters[5].Value = model.GOODS_GROUP_ID;
            return XGWMS.DBUtility.SqlHelper.ExecuteSql(DAL_M006_GOODS_GROUP_SQL.Update_GOODS_GROUP_SQL, parameters);
        }

        /// <summary>	
        /// <p><b>方法名称</b>InsertProcess</p>	
        /// <p><b>概要説明  :</b>商品分类信息的理论删除</p>	
        /// <p><b>作成者    :</b>万岗</p>	
        /// <p><b>作成日    :</b>2008/08/26</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="class" name="model">商品分类信息</param>
        /// <returns type = "ReturnValue"></returns>
        public ReturnValue DeleteProcess(MODEL.MOD_M006_GOODS_GROUP model)
        {
            SqlParameter[] parameters = {		
                    new SqlParameter("@GOODS_GROUP_ID", SqlDbType.Char,12),                    
					new SqlParameter("@UPDATER_ID", SqlDbType.VarChar,12),
					new SqlParameter("@TERMINAL_CD", SqlDbType.VarChar,256)};
            parameters[0].Value = model.GOODS_GROUP_ID;
            parameters[1].Value = model.UPDATER_ID;
            parameters[2].Value = model.TERMINAL_CD;
            return XGWMS.DBUtility.SqlHelper.ExecuteSql(DAL_M006_GOODS_GROUP_SQL.Logic_Delete_GOODS_GROUP_SQL, parameters);
        }

        /// <summary>
        /// <p><b>方法名称</b>InsertProcess</p>	
        /// <p><b>概要説明  :</b>商品分类信息的物理删除</p>	
        /// <p><b>作成者    :</b>万岗</p>	
        /// <p><b>作成日    :</b>2008/08/26</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="class" name="model">商品分类信息</param>
        /// <returns type = "ReturnValue"></returns>
        public ReturnValue PhysicalDeleteProcess(string strGoodsGroupID)
        {
            SqlParameter parameter =
                    new SqlParameter("@GOODS_GROUP_ID", SqlDbType.Char, 12);
            parameter.Value = strGoodsGroupID.Trim();

            return XGWMS.DBUtility.SqlHelper.ExecuteSql(DAL_M006_GOODS_GROUP_SQL.Physical_Delete_GOODS_GROUP_SQL, parameter);
        }

        /// <summary>
        /// 更新排序
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="UPDATER_ID"></param>
        /// <param name="TERMINAL_CD"></param>
        /// <param name="UpOrDown">小于等于0：向上 大于0：向下</param>
        /// <returns></returns>
        public ReturnValue UpdateOrder(string ID,string UPDATER_ID,string TERMINAL_CD,int UpOrDown)
        {
            string strSQL = @"Update M006_GOODS_GROUP 
SET [UPDATE_DATETIME] = CONVERT(VARCHAR,GETDATE(),112) + REPLACE(CONVERT(VARCHAR,GETDATE(),108),':','')
,[TRANS_STATE] = '2'
,[UPDATER_ID] = @UPDATER_ID
,[TERMINAL_CD] = @TERMINAL_CD
,[OrderNum] = OrderNum+@UpOrDown
Where GOODS_GROUP_ID = @GOODS_GROUP_ID

Update M006_GOODS_GROUP 
SET [UPDATE_DATETIME] = CONVERT(VARCHAR,GETDATE(),112) + REPLACE(CONVERT(VARCHAR,GETDATE(),108),':','')
,[TRANS_STATE] = '2'
,[UPDATER_ID] = @UPDATER_ID
,[TERMINAL_CD] = @TERMINAL_CD
,[OrderNum] = OrderNum+@UpOrDown
Where GOODS_GROUP_ID = @GOODS_GROUP_ID";
            SqlParameter[] parameters = {
					new SqlParameter("@UPDATER_ID", SqlDbType.VarChar,12),
					new SqlParameter("@TERMINAL_CD", SqlDbType.VarChar,256),	
                    new SqlParameter("@UpOrDown", SqlDbType.Int),
                    new SqlParameter("@GOODS_GROUP_ID", SqlDbType.Char,12)};
            parameters[0].Value = UPDATER_ID;
            parameters[1].Value = TERMINAL_CD;
            if (UpOrDown > 0)
            {
                parameters[2].Value = 1;
            }
            else if (UpOrDown <= 0)
            {
                parameters[2].Value = -1;
            }
            parameters[3].Value = ID;
            return XGWMS.DBUtility.SqlHelper.ExecuteSql(strSQL, parameters);
        }

        #endregion
    }
}
